package cn.net.isir.service2;

import com.alipay.api.response.AlipayOpenAgentCancelResponse;
import com.alipay.api.response.AlipayOpenAgentConfirmResponse;
import com.alipay.api.response.AlipayOpenAgentCreateResponse;
import com.alipay.api.response.AlipayOpenAgentOrderQueryResponse;
import cn.net.isir.common.pay.alipay.AlipayAPI;
import cn.net.isir.core.BasicService2;
import cn.net.isir.core.SetBasicDao;
import cn.net.isir.dao.AlipayAgentDao;
import cn.net.isir.entity.AlipayAgent;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AlipayAgentService extends BasicService2<AlipayAgent> {
    @SetBasicDao
    @Resource
    private AlipayAgentDao alipayAgentDao;

    /**
     * 开启代理事务
     *
     * @param account        isv代操作的子商户账号，可以是支付宝账号，也可以是pid（2088开头）
     * @param contact_name   子商户联系人名称
     * @param contact_mobile 子商户联系人手机号码
     * @param contact_email  子商户联系人邮箱
     * @return 本次代商户操作的全局唯一事务编号，后续代商户创建小程序、代签约当面付等产品、提交事务等接口都需要传递该batch_no值，且要确认只有 init 状态的batch_no才能发起调用。
     */
    public String alipayOpenAgentCreate(String app_auth_token,
                                        String account,
                                        String contact_name,
                                        String contact_mobile,
                                        String contact_email) {
        AlipayOpenAgentCreateResponse response =
                AlipayAPI.alipayOpenAgentCreate(app_auth_token, account, contact_name, contact_mobile, contact_email, null);
        if (response != null && "init".equals(response.getBatchStatus())) {
            return response.getBatchNo();
        }
        return null;
    }

    /**
     * 提交事务
     *
     * @param batch_no 事务编号
     * @return 提交结果：true表示提交成功；false表示提交失败
     */
    public boolean alipayOpenAgentConfirm(String batch_no) {
        AlipayOpenAgentConfirmResponse response =
                AlipayAPI.alipayOpenAgentConfirm(batch_no);
        if (response != null) {
            return true;
        }
        return false;
    }

    /**
     * 取消事务
     *
     * @param batch_no 事务编号
     */
    public boolean alipayOpenAgentCancel(String batch_no) {
        AlipayOpenAgentCancelResponse response =
                AlipayAPI.alipayOpenAgentCancel(batch_no);
        if (response != null) {
            return true;
        }
        return false;
    }

    /**
     * 查询事务状态
     *
     * @param batch_no 事务编号
     */
    public AlipayOpenAgentOrderQueryResponse alipayOpenAgentOrderQuery(String batch_no) {
        AlipayOpenAgentOrderQueryResponse response =
                AlipayAPI.alipayOpenAgentOrderQuery(batch_no);
        return response;
    }
}
